package genes;

import java.util.Iterator;
import java.util.List;


public class Codon {
	
	private String codon;
	private String AminoAcid;
	private AminoAcids lookup;

	
	public Codon(String codon) {
		lookup= new AminoAcids ();
		if(codon.length()==3){
		this.codon = codon.toUpperCase();
	//	System.out.println(this.codon);
		this.AminoAcid=lookup.getAminoAcid(this.codon);
		}
		
	}
	
	public Codon(Codon codon){
		lookup= new AminoAcids ();
		String s= codon.getCodon();
		if(s.length()==3){
		this.codon = s.toUpperCase();
	//	System.out.println(this.codon);
		this.AminoAcid=lookup.getAminoAcid(s);
		}
	}
	
	public Codon(char x, char y, char z) {
		lookup= new AminoAcids ();
		String codon=String.valueOf(x)+String.valueOf(y)+String.valueOf(z);
		this.codon = codon.toUpperCase();
		this.AminoAcid=lookup.getAminoAcid(this.codon);
	}

	public Codon() {
		//Nothing
	}

	public String codesFor(){
		return AminoAcid;
	}
	
	public String codesFor(String x){
		return lookup.getAminoAcid(x);
	}
	
	public boolean codesSameAminoAcid(Codon x){
		if(x.codesFor()==null){
			return false;
		}else if(x.codesFor()=="nul"){
			return false;
		}else
		return this.codesFor().equals(x.codesFor());
	}
	
	public String getCodon() {
		return codon;
	}
	
	public String getFirstPos(){
		return String.valueOf(this.codon.charAt(0));
	}
	
	public String getSecondPos(){
		return String.valueOf(this.codon.charAt(1));
	}
	
	public String getThirdPos(){
		return String.valueOf(this.codon.charAt(2));
	}

	

	
	
	public boolean contains(List<String> list){
		boolean contains= false;
		Iterator<String> iter= list.iterator();
		while(iter.hasNext()){
			String current = (String) iter.next();
			contains =this.anymatch(current);
			if(contains==true){
				return contains;
			}
		}
		
		return contains;
	}
	
	private boolean anymatch(String x){
		if(x.equalsIgnoreCase(this.getFirstPos())){
		return true;
	}
		else if(x.equalsIgnoreCase(this.getSecondPos())){
			return true;
		}
		else if(x.equalsIgnoreCase(this.getThirdPos())){
			return true;
		}
		return false;
}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((AminoAcid == null) ? 0 : AminoAcid.hashCode());
		result = prime * result + ((codon == null) ? 0 : codon.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Codon other = (Codon) obj;
		if (AminoAcid == null) {
			if (other.AminoAcid != null)
				return false;
		} else if (!AminoAcid.equals(other.AminoAcid))
			return false;
		if (codon == null) {
			if (other.codon != null)
				return false;
		} else if (!codon.equals(other.codon))
			return false;
		return true;
	}
	
	



}
